package com.itheima.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.itheima.*;
import com.itheima.bean.Category;
import com.itheima.bean.Dish;
import com.itheima.bean.Params;
import com.itheima.bean.Setmeal;
import com.itheima.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryDao categoryDao;
   @Autowired
    private DishDao dishDao;
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private DishFlavorDao dishFlavorDao;

    @Override
    public List<Category> findfindUserPage() {
      /*  List<DishDto>  list=new ArrayList<>();
        for (Category category : categoryDao.selectList(null)) {
            DishDto dishDto = new DishDto();
            dishDto.setCategoryName(category.getName());
            LambdaQueryWrapper<Dish> dqw=new LambdaQueryWrapper<>();
            dqw.eq(Dish::getCategoryId,category.getId());
         List<Dish> dishList=  dishDao.selectList(dqw);
            for (Dish dish : dishList) {
                BeanUtils.copyProperties(dish,dishDto);
                LambdaQueryWrapper<DishFlavor> dfqw=new LambdaQueryWrapper<>();
                dfqw.eq(DishFlavor::getDishId,dish.getId());
              List<DishFlavor> dflist= dishFlavorDao.selectList(dfqw);
              dishDto.setFlavors(dflist);
            }
            list.add(dishDto);
        }

        return list;*/
        return categoryDao.selectList(null);
    }

    @Override
    public int add(Category category) {

        return   categoryDao.insert(category);

    }

    @Override
    public IPage<Category> findPage(Params params) {
        LambdaQueryWrapper<Category> qw=new LambdaQueryWrapper<>();
        qw.orderByAsc(Category::getSort).orderByAsc(Category::getName);
        int page = params.getPage();
        int pageSize = params.getPageSize();
        Page<Category> categoryPage = new Page<>(page,pageSize);
      return   categoryDao.selectPage(categoryPage,qw);
    }

    @Override
    public int delete(Long id) {
        LambdaQueryWrapper<Dish> qw1=new LambdaQueryWrapper<>();
        qw1.eq(Dish::getCategoryId,id);
        int count1= dishDao.selectCount(qw1);
        if (count1>0){
            throw new CustomException("当前分类关联了菜品，禁止删除！");
        }
        LambdaQueryWrapper<Setmeal> qw2=new LambdaQueryWrapper<>();
        qw2.eq(Setmeal::getCategoryId,id);
        int count2=setmealDao.selectCount(qw2);
        if (count2>0){
            throw new CustomException("当前分类关联了套餐，禁止删除！");
        }
/*        if (count2>0||count1>0){
            return -1;
        }else {
            int row = categoryDao.deleteById(id);
          return row>0?1:0;
        }*/
        return  categoryDao.deleteById(id);
    }

    @Override
    public int update(Category category) {
       return categoryDao.updateById(category);
    }

    @Override
    public List<Category> findByType(int type) {
        LambdaQueryWrapper<Category> qw=new LambdaQueryWrapper<>();
        qw.eq(Category::getType,type);
       return categoryDao.selectList(qw);
    }
}
